'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class CreateMetadata

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal directiveStatus As IStatusDirective)
         Dim statusoutput As IStatusOutput
         Dim createMetadata As CodeGenerationSupport.ICreateMetadataDirective
         Dim itemstatus As Utility.SourceControlBase.ItemStatus
         Dim outputFile As String
         Dim xmlDoc As Xml.XmlDocument = Nothing
         Dim SQLServerMetaData As Metadata.SQLExtractMetaData
         createMetadata = CType(node, CodeGenerationSupport.ICreateMetadataDirective)
         outputFile = createMetadata.SinglePass.OutputFile
         SQLServerMetaData = New Metadata.SQLExtractMetaData(createMetadata.Server)
         statusoutput = directiveStatus.AddStatusOutput( _
                  outputFile, _
                  FileTreatment.Unknown)
         Try
            If runUtility.ShouldRun(outputFile, _
                     createMetadata.OutputRule.OutputGenType, _
                     createMetadata.OutputRule.HashWhitespace, _
                     createMetadata.OutputRule.OutputFileType, statusoutput) Then
               Try
                  xmlDoc = SQLServerMetaData.CreateMetaData( _
                                   (createMetadata.SkipStoredProcs), _
                                   createMetadata.SelectPatterns, _
                                   createMetadata.SetSelectPatterns, _
                                   createMetadata.LookupPrefix, _
                                   createMetadata.TargetLanguage, _
                                   createMetadata.TableNamingRule.RemAllPref, _
                                   createMetadata.TableNamingRule.Prefixes, _
                                   createMetadata.TableNamingRule.FriendlyCaps, _
                                   createMetadata.TableNamingRule.RemoveUnderscores, _
                                   createMetadata.TableNamingRule.KeywordTreatment, _
                                   createMetadata.TableNamingRule.BlankTreatment, _
                                   createMetadata.TableNamingRule.RemAllPref, _
                                   createMetadata.TableNamingRule.Prefixes, _
                                   createMetadata.TableNamingRule.FriendlyCaps, _
                                   createMetadata.TableNamingRule.RemoveUnderscores, _
                                   createMetadata.TableNamingRule.KeywordTreatment, _
                                   createMetadata.TableNamingRule.BlankTreatment, _
                                   createMetadata.ViewIDType, _
                                   createMetadata.ViewIDText, _
                                   createMetadata.Database)
               Catch ex As System.Exception
                  statusoutput.AddStatusException("CreateMetadata.Run", "Failure creating SQL XML output", ex)
               End Try
               If xmlDoc IsNot Nothing Then
                  Utility.Tools.MakePathIfNeeded(outputFile)
                  xmlDoc.Save(outputFile)
               End If
            End If
            directiveStatus.CountCreated = 1
            directiveStatus.CountSelected = 1
            directiveStatus.CountOutput = 1
         Catch ex As System.Exception
            statusoutput.AddStatusException("CreateMetadata.Run", "Failure writing to file", ex)
         End Try
         statusoutput.Status = statusoutput.ChildStatus
      End Sub
#End Region

   End Class
End Namespace